'use strict'

angular.module('2order.chart')
    .controller('CreateChartItemsController',['$rootScope','$scope','$http','$location','$window','Charts','Candidates','$routeParams',function($rootScope, $scope, $http, $location, $window, Charts, Candidates, $routeParams){
        $scope.isAttempted = false;
        $scope.items = [];
        $scope.chartId = $routeParams.id;
        $scope.isRetrieving = true; // toggle when retrieving data from server

        // first, get all items saved of this chart
        retrieveAll();
        // main form submit
        $scope.submit = function(){
            $scope.isAttempted = true;
            if($scope.items.length > 0){
                $location.path('/newchart/preview/'+$scope.chartId);
            }
        }

        // add items
        $scope.addItem = function(index){
            // add item for this chart
            // create candidate
            switch($scope.items[index].type)
            {
                // text
                case 'text':
                    var newItem = new ChartDetail({
                        title: $scope.items[index].content.title,
                        paragraph: $scope.items[index].content.paragraph,
                        type: 'text'
                    })
                    break;
            }
            // save it to database
            newItem.position = index;
            newItem.$save(function(response){
                if(response.error){
                    $scope.serverError = response.error;
                }else{
                    // display item as saved item
                    $scope.items[index].itemId = response.id;
                    // add item into chart item list
                    Charts.addItem({'chartId':$scope.chartId},
                        {'detailId':response.id},function(response){
                            if(response.error){
                                $scope.serverError = response.error;
                            }else{
                                // saving complete
                                $scope.items[index].saved = true;
                            }
                        });
                }
            });
        }

        // retrieve all candidates for this chart
        function retrieveAll(){
            // get all candidates for this chart
            Charts.retrieveAllDetails({chartId:$scope.chartId},function(res){
                // update $scope.items
                angular.forEach(res,function(key, value){
                    var chartDetailId = key;
                    // load item
                    ChartDetail.get({
                        id:chartDetailId
                    },function(chartDetail){
                        if(chartDetail){
                            var item = {};
                            // initialize
                            item.content = chartDetail.content;
                            item.candidateId = chartDetail.candidate;
                            item.chartDetailId = chartDetail._id;
                            item.saved = true;
                            item.type = chartDetail.type;
                            // add to binding list
                            this.push(chartDetail);
                        }
                    });
                },$scope.items);
            });
        }
    }]);

angular.module('2order.chart')
    .controller('CreateChartIntroController', ['$rootScope','$scope', '$http', '$location', '$window', 'Charts', function ($rootScope, $scope, $http, $location, $window, Charts) {
        $scope.newChart = {
            type:'open'
        };

        $scope.isServerError = false;
        $scope.serverError = '';

        $scope.submit = function(){
            if(!$scope.newChart.caption){
                $scope.newChart.caption = '';
            }
            if(!$scope.newChart.description){
                $scope.newChart.description = '';
            }
            var chart = new Charts({
                name: $scope.newChart.name,
                caption: $scope.newChart.caption,
                description: $scope.newChart.description,
                type: $scope.newChart.type
            });

            chart.$save(function(response){
                if(response.error){
                    $scope.serverError = response.error;
                    $scope.isServerError = true;
                }else{
                    // redirect to preview page
                    $location.path('/newchart/addItems/'+response.id);
                }
            });
        }
    }]);